<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../resources/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../../../resources/css/public.css" media="all" />
</head>
<body >
<div class="layuimini-container">
<!--搜索条件开始-->
	 <div class="layuimini-main">
		<fieldset class="layui-elem-field layuimini-search">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">关键字</label>
							<div class="layui-input-inline">
								<input type="text" name="title" id="title" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<div class="layui-inline">
								<button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
								<button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
							</div>
						</div>
					</div>
					
				</form>
			</div>
		</fieldset>
		<!--搜索条件结束-->
	
<!--搜索条件结束-->

<!--数据表格开始-->
	
		<a class="layui-btn dept_btn_add" href="javascript:void(0)" id="addDeptBtn"><span class="layui-icon layui-icon-add-1"></span>添加</a>
		<table class="layui-hide" id="DeptTable" lay-filter="DeptTable"></table>
		<div id="DeptRowBar" style="display: none">
			<button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger  dept_btn_delete"><span class="layui-icon layui-icon-delete"></span>删除</button>
			<button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-btn-warm  dept_btn_update"><span class="layui-icon layui-icon-set-sm"></span>更新</button>
		</div>
		<script type="text/html" id="spreadTpl">
		  <input type="checkbox" name="spread" value="{{d.id}}" lay-skin="switch" lay-text="展开|不展开" lay-filter="spreadSwitch" {{ d.spread == 1 ? 'checked' : '' }} >
		</script>
		<script type="text/html" id="availableTpl">
		  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
		</script>

	
	</div>
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
		<div class="layui-form-item" id="div_pid">
		    <label class="layui-form-label">父级部门</label>
		    <div class="layui-input-inline">
				<!--隐藏一个PID 这里用layui-hide 不然reset按钮没用-->
		        <input type="text" name="pid" id="p_id" class="layui-hide">
		        <input type="text" name="pname" id="p_name"  placeholder="请输入父级部门" readonly="readonly" autocomplete="off" class="layui-input">
		    </div>
			
			<div class="layui-input-inline">
			   <a class="layui-btn layui-btn-warm" id="doSelectDept"><label class="layui-icon layui-icon-male"></label>选择</a>
			</div>
		</div>
        <div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">部门名称</label>
				<div class="layui-input-inline">
					<!--隐藏一个ID  这里用layui-hide 不然reset按钮没用-->
					<input type="text" name="id"  class="layui-hide">
					<input type="text" name="title"  placeholder="请输入公告标题" lay-verify="required" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">部门地址</label>
				<div class="layui-input-inline">
				  <input type="text" name="address"  autocomplete="off" placeholder="请输入部门地址" class="layui-input">
				</div>
			</div>
        </div>
		
		
		 
		 <div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">创建时间</label>
				<div class="layui-input-inline">
				  <input type="text" name="createtime" id="createtime" lay-verify="required" readonly="readonly" autocomplete="off" placeholder="请选择创建时间" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">排序码</label>
				<div class="layui-input-inline">
				  <input type="text" name="ordernum" id="ordernum" lay-verify="required"  autocomplete="off" placeholder="请输入排序码" class="layui-input">
				</div>
			</div>
		  </div>
		  
		  <div class="layui-form-item">
		  	<label class="layui-form-label">部门备注</label>
		  	<div class="layui-input-block">
		  		  <textarea placeholder="请输入部门备注"  name="remark" class="layui-textarea"></textarea>
		  	</div>
		   </div>
        
        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn"  class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<script src="../../../../resources/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../../resources/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../../resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../../resources/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/common.js" charset="utf-8"></script>
<script src="../../../../resources/common/selectLayer.js" charset="utf-8"></script>

<script>
    layui.use(['layer','form','table','treeTable','laydate'],function () {
        var form=layui.form;
        var layer=layui.layer;
        var table=layui.table;
		var treeTable = layui.treeTable;
		var laydate = layui.laydate;
       
		   //渲染时间选择器
		   laydate.render({
			   elem:'#createtime',
			   type:'datetime'
		   })
       
  

        var tableIndex = treeTable.render({
            tree: {
				iconIndex: 1,  // 折叠图标显示在第几列
				idName: 'id',  // 自定义id字段的名称
				pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
				isPidData: true  // 是否是pid形式数据
			 },
			elem: '#DeptTable',
			cellMinWidth:true,
			cols: [
				{type: "numbers"},
				{field: 'title',  title: '部门名称',width: 300},
				{field: 'id', title: 'ID', align: "center",width: 80},
				{field: 'pid', title: 'PID', align: "center",width: 80},
				{field: 'remark',  title: '部门备注', align: "center",width: 200},
				{field: 'address',  title: '部门地址', align: "center",width: 120},
				{field: 'createtime',  title: '发布时间', align: "center",width: 180},
				{field: 'ordernum',  title: '排序码', align: "center",width: 100},
				//{field: 'spread',  title: '是否展开', align: "center",width: 100,templet:function(d){
				// 	return d.spread==0?"<font color=red>不展开</font>":"<font color=blue>展开</font>"
				// }},
				{field: 'spread',  title: '是否展开', align: "center",width: 120,templet:'#spreadTpl'},
	            //  {field: 'available',  title: '是否可用', align: "center",width: 100,templet:function(d){
				// 	return d.available==0?"<font color=red>不可用</font>":"<font color=blue>可用</font>"
				// }},
				{field: 'available',  title: '是否可用', align: "center",width: 120,templet:'#availableTpl'},
				{title: '操作', templet: '#DeptRowBar', fixed: "right", align: "center",width: 300}
			],
			 reqData: function(data, callback) {
				// 在这里写ajax请求，通过callback方法回调数据 加载treetable数据
				$.get(api+'dept/loadAllDept', function (res) {
					callback(res.data);  // 参数是数组类型
				});
			}

            
        });

        //模糊过滤 前端里面
        form.on('submit(doSearch)',function(data){
            var params = data.field.title;
            if(params){
				tableIndex.filterData(params);
			}else{
				tableIndex.reload();
			}

            return false;
        });

		 //监听表单元素的事件
		 //监听展开操作
		  form.on('switch(spreadSwitch)', function(obj){
			 $.post(api+"dept/updateDept",{id:this.value,spread:obj.elem.checked?1:0},function(res){
				 layer.msg(res.msg);
				 tableIndex.reload();
			 })
		    //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
		  });
		  //监听可用操作
	      form.on('switch(availableSwitch)', function(obj){
	         $.post(api+"dept/updateDept",{id:this.value,available:obj.elem.checked?1:0},function(res){
				  layer.msg(res.msg);
				  tableIndex.reload();
			 })
		    //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
	     });

        //监听行工具条的事件
        treeTable.on("tool(DeptTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteDept(data);
                    break;
                case 'update':
                    UpdateDept(data);
                    break;
                
            };
        });
		
		
		
        var url;
        var mainIndex;

		//添加按钮
		$("#addDeptBtn").click(function(){
			openAddDept();
		})
		
        //添加添加页面
        function openAddDept() {
            mainIndex = layer.open({
                type:1,
                title:'添加部门',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    $("#dataFrm")[0].reset();
                    url= api+"dept/addDept";
					//查找最大排序码
					$.get(api + "dept/queryDeptMaxOrderNum",function(res){
						$("#ordernum").val(res.data);
					})
                }
            });
        }
       
        //更新页面
       function UpdateDept(data){
           mainIndex = layer.open({
               type:1,
               title:'修改部门',
               content:$("#saveOrUpdateDiv"),
               area:['800px','550px'],
               success:function(index){
				   if(data.pid==0){
					   //说明是根部门
					   $("#div_pid").hide();
				   }else{
					   $("#div_pid").show();
					   $.get(api+"dept/getDeptById",{id:data.pid},function(res){
					   		$("#p_name").val(res.data.title);		   
					   })
				   }
                   form.val("dataFrm",data);
				 
                   url= api+"dept/updateDept";
               }
           });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });

       

        //删除
        function  DeleteDept(data) {
			$.get(api+"dept/getDeptChildrenCountById",{id:data.id},function(res1){
				if(res1.data>0){
					layer.msg("请先删除子部门");
				}else{
					layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
					    $.get( api+"dept/deleteDept",{id:data.id},function(res){
					        if(res.code==200){
					            tableIndex.reload();
					        }
					        layer.msg(res.msg);
					    })
					    layer.close(index);
					});
				}
			})
            
        }

		//监听部门选择的按钮事件 打开部门选择的弹出层
		$("#doSelectDept").click(function(){
			var p_id = $("#p_id").val();
			openDeptSelecter(p_id,function(id,name){
				$("#p_id").val(id);
				$("#p_name").val(name);
			})
		})
			
    })
</script>
</body>
</html>